/**
 * @file token的读取/清除/设置
 */
// import { isWeixin } from "@/utils/host";
import Toast from "@vant/weapp/dist/toast/toast";
import storage from "@/utils/storage/index.js";
const tokenName = "SECRET_TOKEN";
export const cookieTokenName = "satoken";
export const isLogin = !!localStorage[tokenName];
import { useAppStore } from "@/store";

/**
 * @description 拿到用户登录的token
 * @returns {String} 返回登录凭证，没有token则返回''
 */
export function getToken() {
  return storage.get(cookieTokenName);
}

/**
 * @description 清除token。比如单登录过期，比如接口返回401
 */
export function removeToken() {
  storage.remove(cookieTokenName);
}
export function setToken(token) {
  storage.set(cookieTokenName, token);
}
function saveLoginBefore() {
  const url = location.hash;
  sessionStorage.setItem("loginBefore", url);
}

export const loginRedirect = () => {
  if (getToken()) {
    return true;
  } else {
    uni.navigateTo({
      url: "/pages/wxlogin/index",
    });
    return false;
  }
};
export function exitLogin() {
  Object.keys(localStorage).forEach((key) => {
    if (key != "siteInfo") localStorage.removeItem(key);
  });
}
/**
 * 区分各个项目
 * @returns
 */
export function isCodeImg() {
  if (location.origin.includes("20ai30")) return "20ai30";
  if (location.origin.includes("aijyxms")) return "aijyxms";
  if (location.origin.includes("ou-ying")) return "ou-ying";
}

/**
 * 点击相册选择图片并校验
 * @param {type} 图片类型
 * @returns 图片地址
 */
export const imgUploadVerify = (data = {}) => {
  const { sizeType = ['original'], format } = data;
  const promise = new Promise(async (resolve, reject) => {
    const res = await uni.chooseImage({
      count: 1,
      sourceType: ["album"],
      sizeType: sizeType,
      // sizeType: sizeType || ["original", "compressed"],
      // sizeType: ["original"],
    });
    const { width, height, type } = await uni.getImageInfo({
      src: res.tempFilePaths[0],
    });

    if (format) {
      let newFormat
      typeof format === "string" ? (newFormat = format.split(",")) : (newFormat = format)

      if (!newFormat.includes(type)) {
        Toast.fail(`需上传图片的格式为${newFormat.join(',')} `);
        return reject("上传图片格式错误");
      }
    }
    // // 最大尺寸
    // if (width > 4096 || height > 4096) {
    //   Toast.fail("图片尺寸最大为4096x4096");
    //   return reject(res);
    // }
    // // 最小尺寸
    // if (width < 256 || height < 256) {
    //   Toast.fail("图片尺寸不得低于256x256");
    //   return reject(res);
    // }
    // 最大体积
    let maxSize = 1024 * useAppStore().siteInfo.maxUploadFileSize; // test
    if (res.tempFiles[0].size / 1024 > maxSize) {
      Toast.fail(
        `图片体积不能超过${useAppStore().siteInfo.maxUploadFileSize
        }M，请更换图片再尝试`
      );
      return reject(res);
    }

    return resolve(res.tempFilePaths[0]);
  });

  return promise;
};

export function textHistoryFn({ id, text, obj }) {
  debugger;
  switch (id) {
    case 38:
      let textCreateImgHistory =
        uni.getStorageSync("textCreateImgHistory") || [];
      !textCreateImgHistory.includes(text) &&
        textCreateImgHistory.unshift(text);
      uni.setStorageSync("textCreateImgHistory", textCreateImgHistory);
      break;
    case 45:
      let createBgcHistory = uni.getStorageSync("createBgcHistory") || [];
      !createBgcHistory.includes(text) &&
        createBgcHistory.unshift({ text, obj });
      uni.setStorageSync("createBgcHistory", createBgcHistory);
      break;
    case 46:
      let createBgcV2History = uni.getStorageSync("createBgcV2History") || [];
      !createBgcV2History.includes(text) && createBgcV2History.unshift(text);
      uni.setStorageSync("createBgcV2History", createBgcV2History);
      break;
    case 48:
      let vipCreateBgcHistory = uni.getStorageSync("vipCreateBgcHistory") || [];
      !vipCreateBgcHistory.includes(text) &&
        vipCreateBgcHistory.unshift({ text, obj });
      uni.setStorageSync("vipCreateBgcHistory", vipCreateBgcHistory);
      break;

    case 49: // 指令改图
      let commandCreateBgcHistory =
        uni.getStorageSync("commandCreateBgcHistory") || [];
      !commandCreateBgcHistory.includes(text) &&
        commandCreateBgcHistory.unshift({ text, obj });
      uni.setStorageSync("commandCreateBgcHistory", commandCreateBgcHistory);
      break;
  }
}
